package com.manli.common.utils;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonLoop {
	public static String json = "{\"TITLE\":\"Json Title\",\"FORM\":{\"USERNAME\":\"Rick and Morty\"},\"ARRAY\":[{\"FIRST\":\"Rick\"},{\"LAST\":\"Morty\"}]}";
	 
    public static String jsonLoopForURLParam(Object object) {
 
    	String param = "";
    	
        if(object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
                Object o = entry.getValue();
                
                param += entry.getKey()+ "=" +entry.getValue() + "&";
                /*if(o instanceof String) {
                    System.out.println("key:" + entry.getKey() + "，value:" + entry.getValue());
                } else {
                	jsonLoopForURLParam(o);
                }*/
            }
        }
        if(object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            for(int i = 0; i < jsonArray.size(); i ++) {
            	jsonLoopForURLParam(jsonArray.get(i));
            }
        }
        
        return param.substring(0,param.length()-1);
    }
 
    public static void main(String[] args) {
 
        JSONObject jsonObject = JSON.parseObject(json);
        System.out.println(jsonLoopForURLParam(jsonObject));;
    }
}
